<?php
class StoriesController extends AppController {

	var $name = 'Stories';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Story->recursive = 0;
		$this->set('stories', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Story.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('story', $this->Story->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Story->create();
			if ($this->Story->save($this->data)) {
				$this->Session->setFlash(__('The Story has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Story could not be saved. Please, try again.', true));
			}
		}
		$cities = $this->Story->City->find('list');
		$users = $this->Story->User->find('list');
		$this->set(compact('cities', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Story', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Story->save($this->data)) {
				$this->Session->setFlash(__('The Story has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Story could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Story->read(null, $id);
		}
		$cities = $this->Story->City->find('list');
		$users = $this->Story->User->find('list');
		$this->set(compact('cities','users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Story', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Story->del($id)) {
			$this->Session->setFlash(__('Story deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>